axios.defaults.baseURL = 'https://hmajax.itheima.net'

function showToast(msg){
    const myToast=document.querySelector('.my-toast')
    const Toast = new bootstrap.Toast(myToast)
    document.querySelector('.toast-body').innerHTML=msg
    Toast.show()
}
// showToast('注册成功')

function checklogin(){
    const token = localStorage.getItem('token')
    if (token === null){
        showToast('请先登录')
        setTimeout(()=>{
            location.href = './login.html'
        },1000)
        
    }
}


function renderusername(){

    const username =localStorage.getItem('username')

    document.querySelector('.username').innerHTML=username

}

function tuichu(){
    document.querySelector('#logout').addEventListener('click',function(){
        console.log('推出')
        // showToast('成功退出')
        localStorage.removeItem('username')
        localStorage.removeItem('token')
            location='./login.html'

    })
}

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么,比如: 统一设置token
        const token = localStorage.getItem('token')
    if(token){
        config.headers.Authorization=token
    }
    return config;
    }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
    });





// 添加请求拦截器
axios.interceptors.response.use(function (response) {
    // 在发送请求之前做些什么,比如: 统一设置token

    return response.data;
    }, function (error) {
                if(error.response.status == 401){
                    // console.log('推出')
                    showToast('登陆失败')
                    localStorage.removeItem('username')
                    localStorage.removeItem('token')
                        setTimeout(()=>{
                            location='./login.html'
                        },1000);
                }
    // 对请求错误做些什么
    return Promise.reject(error);
    });